document.addEventListener('DOMContentLoaded', () => {
  const btn = document.getElementById('closeButton');
  if (btn) {
    document.getElementById('closeButton').addEventListener('click', () => {
    // 通过postMessage向父窗口发送消息
    window.parent.postMessage({ action: 'closeButton' }, '*');
    });
  }


  document.getElementById('csvFileInput').addEventListener('change', function (e) {
            const file = e.target.files[0];
            if (!file) return;

            const reader = new FileReader();
            reader.onload = function (e) {
                const csvText = e.target.result;
                const results = Papa.parse(csvText, {
                    header: false,
                    skipEmptyLines: false
                });
                const data = results.data;
                let newCleanedArr = []
                const cleanedArr = data.map(subArr => subArr.filter(item => item !== '')).filter(it => it.length !== 0)

                const remainingData = cleanedArr.slice(1);

                // 2. 遍历剩余数组，处理长度为3的子数组
                const result = remainingData.map(subArr => {
                    if (subArr.length === 3) {
                        // 3. 删除第一个元素
                        return subArr.slice(1);
                    }
                    // 不满足长度为3的，保持原样
                    return subArr;
                });

                // 3. 将数据遍历 英文变更大写
                const en = {};
                const zh = {};
                result.forEach(subArr => {
                    subArr.forEach(item => {
                        // 只处理英文字符串（假设英文字符串都在子数组中）
                        if (typeof item === 'string' && /[A-Za-z]/.test(item)) {
                            const original = item;
                            // 转换成大写
                            let key = original.toUpperCase();
                            // 替换空格和特殊字符为下划线
                            key = key.replace(/[\s\-]+/g, '_').replace(/[^\w]/g, '');
                            // 赋值到对象
                            en[key] = original;
                            zh[key] = subArr[0];
                        }
                    });
                });
                console.log(en, zh)
                document.getElementById('En').value = JSON.stringify(en);
                document.getElementById('Zh').value = JSON.stringify(zh);
            };
            reader.readAsText(file);
  });

  // 复制英文 中文
    document.getElementById('copyEn').addEventListener('click', function (e) {
    const textarea = document.getElementById('En');
    textarea.select(); // 选中内容
    try {
        const successful = document.execCommand('copy');
        if (successful) {
        alert('内容已复制到剪贴板！');
        } else {
        alert('复制失败');
        }
    } catch (err) {
        alert('不支持此操作：' + err);
    }
  })
});

  document.getElementById('copyZh').addEventListener('click', function (e) {
    const textarea = document.getElementById('Zh');
    textarea.select(); // 选中内容
    try {
        const successful = document.execCommand('copy');
        if (successful) {
        alert('内容已复制到剪贴板！');
        } else {
        alert('复制失败');
        }
    } catch (err) {
        alert('不支持此操作：' + err);
    }
  })
